package cn.bonoon.controllers.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.core.plugins.QuestionService;
import cn.bonoon.entities.interacting.QuestionEntity;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.models.JsonResult;
import cn.bonoon.util.UrlKeyHelper;

@Controller
@RequestMapping("s/user/ask")
public class UserAskController extends AbstractController {
	
	private final QuestionService questionService;

	@Autowired
	public UserAskController(QuestionService questionService) {
		this.questionService = questionService;
	}

	@RequestMapping("!{key}/index.do")
	public String get(Model model, @PathVariable("key") String key) {
		Long page = 1L;
		List<QuestionEntity> question = questionService.asks(getUser(), page);
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (QuestionEntity entity : question) {
			sb.append("<tr><td style='width:100%;padding-top:10px;'><font style='line-height:150%;font-weight:bold;font-size:12px;'>")
				.append(++i).append('.').append(entity.getQuestion())
				.append("</font></td></tr><tr><td style='width:100%;padding-bottom:10px;'><font style='line-height:150%;font-size:12px;'>")
				.append(entity.isAnswered() ? entity.getAnswer() : "未回答！").append("</font></td></tr>");
		}
		model.addAttribute("content", sb);
		model.addAttribute("page", page);
		model.addAttribute("key", UrlKeyHelper.filter(key));
		return "user/user-ask";
	}

	@ResponseBody
	@RequestMapping(value = "load", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult announcementLoad(Model model, Long page) {
		List<QuestionEntity> question = questionService.asks(getUser(), ++page);
		StringBuffer sb = new StringBuffer();
		int i = (int) (page - 1) * 5;
		for (QuestionEntity entity : question) {
			sb.append("<tr><td style='width:100%;padding-top:10px;'><font style='line-height:150%;font-weight:bold;font-size:12px;'>")
				.append(++i).append('.').append(entity.getQuestion())
				.append("</font></td></tr><tr><td style='width:100%;padding-bottom:10px;'><font style='line-height:150%;font-size:12px;'>")
				.append(entity.getAnswer()).append("</font></td></tr>");
		}
		return JsonResult.result(sb.toString());
	}

	@ResponseBody
	@RequestMapping(value = "save", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult announcementSave(Model model, String contactName, String contactPhone, String contactEmail, String question) {
		LogonUser user = getUser();
		QuestionEntity entity = new QuestionEntity();
		entity.setContactEmail(contactEmail);
		entity.setContactName(contactName);
		entity.setContactPhone(contactPhone);
		entity.setCreateAt(new Date());
		entity.setCreatorId(user.getId());
		entity.setCreatorName(user.getUsername());
		entity.setAnswered(false);
		entity.setQuestion(question);
		questionService.save_ask(entity);
		return JsonResult.result();
	}
}
